<template>
  <el-dialog :title="$t('productCenter.product.editSettlementRules')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-row v-for="(item, index) in $t('productCenter.product.type1')" :key="index">
        <p>{{ item['desc'] }}</p>
        <el-form-item :label="$t('productCenter.product.list.automaticSettlementRatio')" :prop="item['formControlName']">
          <el-col :span="20">
            <el-input-number
              v-model="formValidate[item['formControlName']]"
              style="width: 100%"
              :step="0.01"
              :precision="2"
              :max="100.00"
              :min="0.01"
              :placeholder="$t('productCenter.product.promptRatio')"
            />
          </el-col>
          <el-col :span="4" style="padding-left: 10px">%</el-col>
        </el-form-item>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('BusinessConfig.machineReview.submit') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateSettleRule } from '@/api/product'
export default {
  name: 'SettleRuleDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        onceContainsServiceFee: null,
        oncePreTakeOffServiceFee: null,
        periodContainsServiceFee: null,
        periodPreTakeOffServiceFee: null
      },
      rules: {
        onceContainsServiceFee: [{ required: true, type: 'number', message: this.$t('productCenter.product.list.automaticSettlementRatio') + this.$t('common.null'), trigger: 'blur' }],
        oncePreTakeOffServiceFee: [{ required: true, type: 'number', message: this.$t('productCenter.product.list.automaticSettlementRatio') + this.$t('common.null'), trigger: 'blur' }],
        periodContainsServiceFee: [{ required: true, type: 'number', message: this.$t('productCenter.product.list.automaticSettlementRatio') + this.$t('common.null'), trigger: 'blur' }],
        periodPreTakeOffServiceFee: [{ required: true, type: 'number', message: this.$t('productCenter.product.list.automaticSettlementRatio') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', 'isRules', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          updateSettleRule(this.formValidate)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('common.tips.operateSuccess'))
                this.$emit('change-dialog', 'isRules', false)
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
p{
  text-align: center;
}
</style>
